/*
 * @Description: 小于等于1920分辨率下的响应式方案
 * @Version: 1.0
 * @Autor: zhaozhenzhuo
 * @Date: 2025-10-21 16:53:30
 * @LastEditors: zhaozhenzhuo
 * @LastEditTime: 2025-10-21 16:53:30
 */
/* eslint-disable */
(function (designWidth: number, maxWidth: number, minWidth: number) {
    const doc = document;
    const win = window;
    const docEl = document.documentElement;
    let tid: NodeJS.Timeout;
    let rootItem: HTMLElement | HTMLStyleElement | null;
    let rootStyle;

    function refreshRem() {
        let width = document.getElementById("app")?.getBoundingClientRect().width;
        let rem;
        if (!maxWidth) {
            maxWidth = 1920;
        }
        if (!minWidth) {
            minWidth = 1366;
        }
        if (width && width > maxWidth) {
            if (width <= 2560) {
                rem = 1.1;
            } else {
                rem = 1.2;
            }
        } else if (width && width < minWidth) {
            // rem = (minWidth * 1) / designWidth;
            rem = 0.9;
        } else {
            // 与淘宝做法不同，直接采用简单的rem换算方法1rem=1px
            rem = ((width && width > 0 ? width : 1920) * 1) / designWidth;
        }
        // 兼容UC开始
        rootStyle = `html{font-size:${rem}px !important}`;
        rootItem = document.getElementById("rootsize") || document.createElement("style");
        if (!document.getElementById("rootsize")) {
            document.getElementsByTagName("head")[0].appendChild(rootItem);
            rootItem.id = "rootsize";
        }
        if (rootItem && (rootItem as any).styleSheet) {
            (rootItem as any).styleSheet.disabled ||
                ((rootItem as any).styleSheet.cssText = rootStyle);
        } else {
            try {
                rootItem.innerHTML = rootStyle;
            } catch (f) {
                rootItem.innerText = rootStyle;
            }
        }
        // 兼容UC结束
        docEl.style.fontSize = `${rem}px`;
    }
    refreshRem();

    win.addEventListener(
        "resize",
        function () {
            clearTimeout(tid); // 防止执行两次
            tid = setTimeout(refreshRem, 300);
        },
        false
    );

    win.addEventListener(
        "pageshow",
        function (e) {
            if (e.persisted) {
                // 浏览器后退的时候重新计算
                clearTimeout(tid);
                tid = setTimeout(refreshRem, 300);
            }
        },
        false
    );
})(1920, 1920, 1366);
